<template>
  <el-dialog
    :before-close="beforeClose"
    :visible.sync="show"
    :title="title"
    width="30%"
    @close="close"
    @closed="closed"
  >
    <el-form ref="form" label-width="80px" :model="form">
      <el-form-item label="账号" required>
        <el-input v-model="form.account" />
      </el-form-item>
      <el-form-item label="真实姓名" required>
        <el-input v-model="form.realName" />
      </el-form-item>
      <el-form-item label="密码" :required="type === 'create'">
        <el-input v-model="form.password" class="k-flex-1 " placeholder="输入密码将修改密码，空不修改" />
        <!--        <div class="k-box-flex">-->
        <!--          <el-input v-model="form.resetPass" class="k-flex-1 " />-->
        <!--          <el-button v-if="type === 'edit'" type="primary" class="k-ml-10 k-width-100-p" @click="handleResetPass">重置密码</el-button>-->
        <!--        </div>-->
      </el-form-item>
      <el-form-item label="手机号" required>
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="身份证">
        <el-input v-model="form.idCard" />
      </el-form-item>
      <el-form-item label="状态">
        <el-switch
          v-model="form.disable"
          :active-value="0"
          :inactive-value="1"
          active-color="#13ce66"
          @change="handleDisable"
        />
      </el-form-item>
      <el-form-item label="角色" required>
        <el-select v-model="form.roleId" placeholder="请选择" class="k-width-100">
          <el-option
            v-for="item in roleList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { editUser, addUser } from '@/api/system'
import select from '@/list/select'
export default {
  name: 'Index',
  mixins: [select],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'view'
    },
    data: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      show: false,
      radio: '1',
      form: {
        'id': '',
        'userName': '',
        'realName': '',
        'password': '',
        'mobile': '',
        'idCard': '',
        'disable': '',
        'roleId': ''
      }
    }
  },
  computed: {
    title() {
      let text = ''
      switch (this.type) {
        case 'view':
          text = '查看'
          break
        case 'create':
          text = '新建'
          break
        case 'edit':
          text = '编辑'
          break
      }
      return text + '用户'
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.show = val
        if (this.type === 'edit') {
          this.form = this.data
        } else {
          this.form = {
            'id': '',
            'userName': '',
            'realName': '',
            'password': '',
            'mobile': '',
            'idCard': '',
            'disable': '',
            'roleId': ''
          }
        }
      }
    }
  },
  mounted() {
    this.show = this.visible
    this.getRole()
  },
  methods: {
    handleResetPass() {
      console.log('重置密码')
    },
    handleDisable(value) {
      console.log('切换状态', value)
    },
    handleChange(value) {
      console.log(value)
    },
    getData() {
      console.log('获取数据')
    },
    handleClose() {
      console.log('点击关闭')
      this.beforeClose()
    },
    handleSubmit() {
      console.log('点击确定', this.form)
      if (this.type === 'edit') {
        editUser(this.form).then(res => {
          console.log('结果', res)
          this.$notify.success('保存成功')
          this.$emit('refresh')
          this.handleClose()
        })
      }
      if (this.type === 'create') {
        this.form.userName = this.form.account
        addUser(this.form).then(res => {
          console.log('结果', res)
          this.$notify.success('保存成功')
          this.$emit('refresh')
          this.handleClose()
        })
      }
    },
    beforeClose() {
      console.log('关闭前的回调')
      this.show = false
      this.$emit('update:visible', false)
    },
    close() {
      console.log('关闭的回调')
    },
    closed() {
      console.log('关闭动画结束后的回调')
    }
  }
}
</script>

<style scoped>

</style>
